<?php

namespace App\Providers;

use App\Http\Services\AuthService;
use Illuminate\Support\ServiceProvider;

class AdminServiceProvider extends ServiceProvider
{

    protected array $middlewareGroups = [
        'admin-api' => [
            \App\Http\Middleware\AdminAuthenticate::class,
            \App\Http\Middleware\AdminPermission::class,
        ],
    ];

    /**
     * @throws \Illuminate\Contracts\Container\BindingResolutionException
     */
    public function register()
    {
        $this->registerRouteMiddleware();

        $this->app->singleton('admin-user', fn($app) => new AuthService($app));
    }

    public function boot()
    {
        $this->loadRoutesFrom(base_path('routes' . DIRECTORY_SEPARATOR . 'admin-api.php'));
    }

    /**
     * 路由中间件注册.
     * @throws \Illuminate\Contracts\Container\BindingResolutionException
     */
    protected function registerRouteMiddleware(): void
    {
        $router = $this->app->make('router');

        foreach ($this->middlewareGroups as $key => $middleware) {
            $router->middlewareGroup($key, $middleware);
        }

    }
}
